﻿
using System.Collections.Generic;
using System.Linq;

using OpenCvSharp;

using Xc.DataAccess.CodeReader.Properties;
using Xc.DataAccess.Core;

namespace Xc.DataAccess.CodeReader.Models.Filters
{
    /// <summary>
    ///解码二维码过滤器
    /// </summary>
    [Filter("解码二维码",FilterType.Algorithm)]
    public class AlgorithmQrCodeDecode : FilterBase
    {
        private CVPointFloat[] point2Fs = new CVPointFloat[] { };

        string deCodeInfo;
        /// <summary>
        /// 检测到的信息
        /// </summary>
        [OutputParameter("检测到的条码信息")]
        public string BarCode
        {
            get => deCodeInfo;
            set
            {
                if (deCodeInfo == value)
                {
                    return;
                }

                deCodeInfo = value;
                RaisedPropertyChanged();
            }
        }
        /// <summary>
        /// 坐标信息
        /// </summary>
        [OutputParameter("条码坐标信息")]
        public CVPointFloat[] Points
        {
            get => point2Fs; set
            {
                if (point2Fs == value)
                {
                    return;
                }

                point2Fs = value;
                RaisedPropertyChanged();
            }
        }
        /// <summary>
        /// 颜色
        /// </summary>
        [InputParameter("颜色")]
        public Scalar Color { get; set; }
        /// <summary>
        /// 线条类型
        /// </summary>
        [InputParameter("线条类型")]
        public LineTypes LineTypes { get; set; } = LineTypes.Link8;
        /// <summary>
        /// 粗细
        /// </summary>
        [InputParameter("粗细", ParameterType.Optional)]
        public int Thickness { get; set; } = 1;
        /// <summary>
        /// 使用管道
        /// </summary>
        /// <param name="mat"></param>
        /// <param name="filterBases"></param>
        /// <param name="preFilterBase"></param>
        /// <returns></returns>
        internal override ResultData<Mat> UseChannel(Mat mat,FilterBase[] filterBases, FilterBase preFilterBase)
        {
            try
            {
                QRCodeDetector qRCodeDetector = new QRCodeDetector();
                var barcode = qRCodeDetector.DetectAndDecode(mat, out var points);
                if (!string.IsNullOrEmpty(barcode))
                {
                    BarCode = barcode;
                    Points = points.Select(p=>(CVPointFloat)p).ToArray();
                    DrawContours drawContours = new DrawContours();
                    drawContours.Points =new List<Point[]> { points.Select(p => (Point)p).ToArray() };
                    drawContours.Thickness = Thickness;
                    drawContours.LineTypes = LineTypes;
                    drawContours.Color = Color;
                    return drawContours.Use(mat,filterBases, preFilterBase);
                }
                return ResultData<Mat>.Accept(TextResource.detect_failed, mat);

            }
            catch (System.Exception err)
            {
                 return ResultData<Mat>.Reject(err.Message,default);

            }

        }
    }
}
